/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eu.ict_omelette.mashup.compositionminer.util;

import eu.ict_omelette.mashup.canonicalmodel.CanonicalMashup;
import eu.ict_omelette.mashup.canonicalmodel.Component;
import eu.ict_omelette.mashup.canonicalmodel.Dataflow;
import eu.ict_omelette.mashup.canonicalmodel.InputParameter;
import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 *
 * @author carlos
 */
public class Util {

    
    
    public static String getGraphRepresentation(CanonicalMashup cm){

        String strGraph = "";

        LinkedHashMap<String, Integer> compToIdx = new LinkedHashMap();
        LinkedHashMap<Integer, String> idxToComp = new LinkedHashMap();

        int counter = 0;
        Iterator<Component> itComp = cm.getComponents();
        while(itComp.hasNext()){
            Component comp = itComp.next();

            Integer idx = compToIdx.get(comp.getType());
            if(idx == null){
                counter++;
                compToIdx.put(comp.getType(), counter);
                idxToComp.put(counter, comp.getType());
            }            
        }

        for(int i = 1; i <= idxToComp.size(); i++){
            String compType = idxToComp.get(i);
            strGraph += "v " + i + " " + compType + "\n";
        }

        Iterator<Dataflow> itDf = cm.getDataFlows();
        while(itDf.hasNext()){
            Dataflow df = itDf.next();

            String fromCompType = df.getFromComponent().getType();
            String toCompType = df.getToComponent().getType();

            String fromCompPort = df.getFromPort().getName();
            String toCompPort = df.getToPort().getName();

            int fromCompIdx = compToIdx.get(fromCompType);
            int toCompIdx = compToIdx.get(toCompType);

            strGraph += "e " + fromCompIdx + " " + toCompIdx + " " +
                        fromCompType + "." + fromCompPort + "=>" +
                        toCompType + "." + toCompPort +
                        "\n";            
        }

        strGraph += "g " + cm.getId();

        

        return strGraph;

    }
    
    public static void printCanonicalMashup(CanonicalMashup cm){
        Iterator<Component> itComp = cm.getComponents();
        System.out.println("Components: ");
        while(itComp.hasNext()){
            Component comp = itComp.next();
            printComponent(comp, "\t");
        }

        Iterator<Dataflow> itDfs = cm.getDataFlows();
        System.out.println("\nWires:");
        while(itDfs.hasNext()){
            Dataflow df = itDfs.next();
            System.out.println("\t(" + df.getFromComponent().getType() + ")" +  df.getFromPort().getName() + "=>" + df.getToComponent().getType() + "("  + df.getToPort().getName() +")");

        }
    }

    public static void printComponent(Component comp, String tabLevel){
        System.out.println(tabLevel + comp.getName());

        Iterator<InputParameter> ipIt = comp.getInputParameters();
        while(ipIt.hasNext()){
            InputParameter ip = ipIt.next();
            System.out.println(tabLevel + "\t" + ip.getName() + "=" + ip.getValue() + "(isDataMapping = " + ip.getDataMapping() + ")");
        }

        if(comp.getIsEmbedding()){
            System.out.println(tabLevel + "\tEMBEDDED COMPONENT:");
            Iterator<Component> it = comp.getEmbeddedComponents();
            if(it.hasNext()){
                printComponent(it.next(), tabLevel + "\t\t");
            }
        }


    }


}
